﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Xml.Linq;
using SlamCms.Configuration;

namespace SlamCms.SharePointServices.Configuration
{
	public class SharePointServiceEnvironmentConfiguration : EnvironmentConfiguration
	{
		private SharePointServiceEnvironmentConfigurationNetworkCredential _networkCredential;
		public SharePointServiceEnvironmentConfigurationNetworkCredential NetworkCredential
		{
			get
			{	
				if (_networkCredential == null)
				{
					if (this.ContainsElement("SharePointServices.NetworkCredential"))
					{
						ConfigurationElement networkCredentialElement = this["SharePointServices.NetworkCredential"][0];
						_networkCredential = new SharePointServiceEnvironmentConfigurationNetworkCredential(networkCredentialElement["UserName"],
																											networkCredentialElement["Password"],
																											networkCredentialElement["Domain"],
																											networkCredentialElement["ClaimsAuthenticationType"]);
					}
					else
					{
						_networkCredential = new SharePointServiceEnvironmentConfigurationNetworkCredential();
					}
					
				}
				return _networkCredential;
			}
		}

		private SharePointServiceEnvironmentConfigurationTrustedIdentityProvider _trustedIdentityProvider;
		public SharePointServiceEnvironmentConfigurationTrustedIdentityProvider TrustedIdentityProvider
		{
			get
			{
				if (_trustedIdentityProvider == null)
				{
					if (this.ContainsElement("SharePointServices.TrustedIdentityProvider"))
					{
						ConfigurationElement trustedIdentityElement = this["SharePointServices.TrustedIdentityProvider"][0];
						_trustedIdentityProvider = new SharePointServiceEnvironmentConfigurationTrustedIdentityProvider(trustedIdentityElement["SamlServerUrl"],
																														trustedIdentityElement["StsServerUrl"]);
					}
					else
					{
						_trustedIdentityProvider = new SharePointServiceEnvironmentConfigurationTrustedIdentityProvider();
					}
				}
				return _trustedIdentityProvider;
			}
		}

		public SharePointServiceEnvironmentConfiguration()
			: base()
		{
		}
	}

	public class SharePointServiceEnvironmentConfigurationNetworkCredential
	{
		public string ClaimsAuthenticationType
		{
			get;
			set;
		}

		public string Domain
		{
			get;
			set;
		}

		public string Password
		{
			get;
			set;
		}

		public string UserName
		{
			get;
			set;
		}

		public SharePointServiceEnvironmentConfigurationNetworkCredential()
		{
			UserName = "";
			Password = "";
			Domain = "";
		}

		public SharePointServiceEnvironmentConfigurationNetworkCredential(string username, string password, string domain, string claimsAuthenticationType)
			: this()
		{
			UserName = username;
			Password = password;
			Domain = domain;
			ClaimsAuthenticationType = claimsAuthenticationType;
		}
	}

	public class SharePointServiceEnvironmentConfigurationTrustedIdentityProvider
	{
		public string SamlServerUrl
		{
			get;
			set;
		}

		public string StsServerUrl
		{
			get;
			set;
		}

		public SharePointServiceEnvironmentConfigurationTrustedIdentityProvider()
		{
			SamlServerUrl = "";
			StsServerUrl = "";
		}

		public SharePointServiceEnvironmentConfigurationTrustedIdentityProvider(string samlServerUrl, string stsServerUrl)
			: this()
		{
			SamlServerUrl = samlServerUrl;
			StsServerUrl = stsServerUrl;
		}
	}
}
